package com.zhuss.util;

import java.io.IOException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class Test5 {
	
	public static void main(String[] args) {
		Object obj = HttpPing("cgwps.com", "/pc/wpsFile/14e3f2c11fc94fffa7d6925005199d0f.jpg", "121.199.36.7", "https");
		System.out.println(obj);
	}
	
	
	private static JSONObject HttpPing(String dns, String uri, String qlb, String scheme) {
        String url = scheme + "://" + dns + uri;
        CloseableHttpClient httpclient = null;
        if (scheme.equals("http")) {
            httpclient = getHttpClient(qlb);
        } else if (scheme.equals("https")) {
            httpclient = getHttpsClient(qlb);
        } else{
            return null;
        }
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse httpResp = null;
        try {
            httpResp = httpclient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            int statusCode = httpResp.getStatusLine().getStatusCode();
            if (statusCode == org.apache.http.HttpStatus.SC_OK) {
                return JSONObject.parseObject(EntityUtils.toString(httpResp.getEntity(), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpResp.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
	private static CloseableHttpClient getHttpClient(String qlb) {
        HttpHost proxy = new HttpHost(qlb, 80, "http");
        //把代理设置到请求配置
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setProxy(proxy)
                .build();
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        return httpclient;
    }

    private static CloseableHttpClient getHttpsClient(String qlb) {//这里设置客户端不检测服务器ssl证书
        try {
            X509TrustManager x509mgr = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] xcs, String string) {
                }
                public void checkServerTrusted(X509Certificate[] xcs, String string) {
                }
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { x509mgr }, null);
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            HttpHost proxy = new HttpHost(qlb, 443, "https");
            RequestConfig defaultRequestConfig = RequestConfig.custom()
                    .setProxy(proxy)
                    .build();
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(defaultRequestConfig).build();
            return httpclient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
